/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewBookPanel.java
 *
 * Created on 26-dic-2013, 21.40.35
 */

package APP2.bookshop.GUI;

import APP2.bookshop.book.Book;
import APP2.bookshop.main.BookShop;
import GUI.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author finalfire
 */
public class ViewBookPanel extends javax.swing.JPanel {

    BookShop bookShop;
    DateFormat dateFormat;

    /** Creates new form ViewBookPanel */
    public ViewBookPanel(BookShop bookShop) {
        this.dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        this.bookShop = bookShop;
        initComponents();
    }

    private void updateLists() {
        this.booksList.setModel(this.bookShop.getBookController().getBooksModel());
        this.authorsList.setModel(new DefaultListModel());
        titleField.setText("");
        codeField.setText("");
        publisherField.setText("");
        publishingDateField.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane2 = new javax.swing.JScrollPane();
        booksList = new javax.swing.JList();
        titleLabel = new javax.swing.JLabel();
        codeLabel = new javax.swing.JLabel();
        publisherLabel = new javax.swing.JLabel();
        publishingDateLabel = new javax.swing.JLabel();
        authorsLabel = new javax.swing.JLabel();
        codeField = new javax.swing.JTextField();
        titleField = new javax.swing.JTextField();
        publisherField = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        authorsList = new javax.swing.JList();
        jSeparator2 = new javax.swing.JSeparator();
        deleteBook = new javax.swing.JButton();
        publishingDateField = new javax.swing.JTextField();
        codeLabel1 = new javax.swing.JLabel();
        inStockField = new javax.swing.JTextField();

        jScrollPane1.setViewportView(jTextPane1);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jLabel1.setText("View Books");

        jLabel2.setForeground(new java.awt.Color(51, 51, 51));
        jLabel2.setText("Click on the element in the list to display further details about it.");

        booksList.setModel(this.bookShop.getBookController().getBooksModel());
        booksList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                booksListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(booksList);

        titleLabel.setFont(new java.awt.Font("Lucida Grande", 0, 14));
        titleLabel.setForeground(new java.awt.Color(51, 51, 51));
        titleLabel.setText("Title");

        codeLabel.setFont(new java.awt.Font("Lucida Grande", 0, 14));
        codeLabel.setForeground(new java.awt.Color(51, 51, 51));
        codeLabel.setText("Book code");

        publisherLabel.setForeground(new java.awt.Color(51, 51, 51));
        publisherLabel.setText("Publisher");

        publishingDateLabel.setForeground(new java.awt.Color(51, 51, 51));
        publishingDateLabel.setText("Publishing Date");

        authorsLabel.setForeground(new java.awt.Color(51, 51, 51));
        authorsLabel.setText("Authors");

        codeField.setEditable(false);

        titleField.setEditable(false);

        publisherField.setEditable(false);

        jScrollPane3.setViewportView(authorsList);

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        deleteBook.setText("Delete book");
        deleteBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBookActionPerformed(evt);
            }
        });

        publishingDateField.setEditable(false);

        codeLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 14));
        codeLabel1.setForeground(new java.awt.Color(51, 51, 51));
        codeLabel1.setText("In Stock");

        inStockField.setEditable(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(deleteBook)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 162, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(10, 10, 10)
                        .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(authorsLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .add(publishingDateLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(publishingDateField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .add(codeLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(codeField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                                .add(18, 18, 18)
                                .add(codeLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(inStockField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(publisherLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(publisherField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .add(titleLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(titleField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(titleLabel)
                            .add(titleField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(codeField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(codeLabel)
                            .add(inStockField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(codeLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(publisherLabel)
                            .add(publisherField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(publishingDateLabel)
                            .add(publishingDateField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(authorsLabel)
                                .add(42, 42, 42))))
                    .add(jSeparator2)
                    .add(jScrollPane2))
                .add(18, 18, 18)
                .add(deleteBook)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void booksListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_booksListMouseClicked
        // TODO add your handling code here:
        String title = (String) this.booksList.getSelectedValue();
        System.out.println(title);
        Book b = this.bookShop.getBookController().getBookByTitle(title);

        // update the gui
        authorsList.setModel(this.bookShop.getBookController().getAllAuthorsByBook(b));
        titleField.setText(title);
        codeField.setText(Integer.toString(b.getCode()));
        if (b.getPublisher() == null)
            publisherField.setText("no publisher");
        else
            publisherField.setText(b.getPublisher().toString());
        publishingDateField.setText(dateFormat.format(b.getPublishingDate()));
        inStockField.setText(Integer.toString(b.getStock().getCopy()));
    }//GEN-LAST:event_booksListMouseClicked

    private void deleteBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBookActionPerformed
        if (this.booksList.getSelectedIndex() != -1) {
            int choice = JOptionPane.showConfirmDialog(this,
                    "Are you sure to delete this book?", "Delete book",
                    JOptionPane.YES_NO_OPTION);

            if (choice == JOptionPane.YES_OPTION)
                if(this.bookShop.getBookController().deleteBook(this.bookShop.getBookController().getBookByTitle(this.booksList.getSelectedValue().toString()))) {
                    this.updateLists();
                    JOptionPane.showMessageDialog(this, "Book correctly deleted!");
                }
        }
    }//GEN-LAST:event_deleteBookActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel authorsLabel;
    private javax.swing.JList authorsList;
    private javax.swing.JList booksList;
    private javax.swing.JTextField codeField;
    private javax.swing.JLabel codeLabel;
    private javax.swing.JLabel codeLabel1;
    private javax.swing.JButton deleteBook;
    private javax.swing.JTextField inStockField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextField publisherField;
    private javax.swing.JLabel publisherLabel;
    private javax.swing.JTextField publishingDateField;
    private javax.swing.JLabel publishingDateLabel;
    private javax.swing.JTextField titleField;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables

}
